Skip to content

Add configurable Lambda resources and Function URL support#12

Merged
ncipollina merged 4 commits into
mainfrom
feature/lambda-configurable-resources-and-function-urls
Jul 9, 2025
Merged

Add configurable Lambda resources and Function URL support#12
ncipollina merged 4 commits into
mainfrom
feature/lambda-configurable-resources-and-function-urls

Conversation

@ncipollina

Copy link
Copy Markdown
Contributor

Summary

This PR adds three new configurable properties to the Lambda construct to provide more flexibility and enable direct HTTP access:

  • MemorySize (default: 1024 MB) - Configurable memory allocation instead of hardcoded 1024 MB
  • TimeoutInSeconds (default: 6 seconds) - Configurable timeout instead of hardcoded 6 seconds
  • GenerateUrl (default: false) - Optional Function URL generation for direct HTTP access without API Gateway

Key Features

🔧 Enhanced Lambda Configuration

  • Memory and timeout are now customizable while maintaining backward compatibility
  • Public readonly properties expose LambdaFunction and LiveAliasFunctionUrlDomain for advanced scenarios
  • Function URLs provide direct HTTP access with automatic CloudFormation outputs

🧪 Comprehensive Testing Support

  • Added 6 new assertion methods to LambdaFunctionConstructAssertions
  • Enhanced test customization system with generateUrl parameter
  • Updated fluent props builder with 4 new methods
  • Added 9 comprehensive unit tests covering all new functionality

📖 Developer Experience

  • Updated README with 3 new example sections showcasing the features
  • Enhanced documentation with new configuration options and testing methods
  • Backward compatibility maintained - all existing code continues to work

Examples

Custom Memory and Timeout

var lambda = new LambdaFunctionConstruct(this, "MyLambda", new LambdaFunctionConstructProps
{
    // ... existing props
    MemorySize = 2048,       // Custom memory (default: 1024 MB)
    TimeoutInSeconds = 30,   // Custom timeout (default: 6 seconds)
});

Function URL for Direct HTTP Access

var lambda = new LambdaFunctionConstruct(this, "MyApi", new LambdaFunctionConstructProps
{
    // ... existing props
    GenerateUrl = true,      // Enable Function URL
    MemorySize = 512,        // Optimized for lightweight API
    TimeoutInSeconds = 15,   // API timeout
});

// Access the function URL domain
string url = lambda.LiveAliasFunctionUrlDomain;

Enhanced Testing

// Fluent builder with new methods
var props = CdkTestHelper.CreatePropsBuilder("./lambda.zip")
    .WithMemorySize(2048)
    .WithTimeoutInSeconds(30)
    .WithGenerateUrl()
    .Build();

// New assertion helpers
template.ShouldHaveMemorySize(2048);
template.ShouldHaveTimeout(30);
template.ShouldHaveFunctionUrl();
template.ShouldHaveFunctionUrlOutput("stack", "construct");

Test Plan

  • All 151 existing tests pass - backward compatibility verified
  • 9 new comprehensive unit tests added covering:
    • Configurable memory size and timeout
    • Function URL generation (enabled/disabled scenarios)
    • Public properties exposure
    • Backward compatibility verification
    • Props builder functionality
    • Default value verification
  • Test customization system enhanced with generateUrl parameter
  • New assertion methods working correctly
  • Build succeeds with no warnings or errors
  • README examples verified and tested

Breaking Changes

None - this is a fully backward compatible enhancement. All existing functionality is preserved with sensible defaults.

🤖 Generated with Claude Code

- Add MemorySize (default: 1024 MB) for configurable memory allocation
- Add TimeoutInSeconds (default: 6 seconds) for configurable timeout
- Add GenerateUrl (default: false) for optional Function URL generation
- Expose public LambdaFunction and LiveAliasFunctionUrlDomain properties
- Update LambdaFunctionConstructPropsBuilder with new fluent methods
- Add comprehensive test assertion helpers for new functionality
- Enhance test customization system with generateUrl parameter
- Add 9 comprehensive unit tests covering all new features
- Update README with examples and documentation
- Maintain backward compatibility with sensible defaults

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@ncipollina
ncipollina requested a review from Copilot July 8, 2025 23:32

This comment was marked as outdated.

- Remove (int) cast from MemorySize assignment in LambdaFunctionConstruct
- Remove (int) casts from test assertions for consistency
- AWS CDK MemorySize and Timeout properties accept double directly
- Improves code clarity and maintains type consistency

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@ncipollina
ncipollina requested a review from Copilot July 8, 2025 23:39

This comment was marked as outdated.

- Remove ShouldHaveCustomResourceConfiguration() method that duplicated ShouldHaveMemorySize and ShouldHaveTimeout
- Remove WithoutGenerateUrl() method in favor of WithGenerateUrl(bool) parameter
- Update test files to use consolidated WithGenerateUrl(false) instead of WithoutGenerateUrl()
- All 151 tests pass successfully after changes

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@ncipollina
ncipollina requested a review from Copilot July 9, 2025 00:02

This comment was marked as outdated.

- Remove ShouldHaveCustomResourceConfiguration reference (method was removed)
- Remove WithoutGenerateUrl reference (method was removed in favor of WithGenerateUrl(bool))
- Documentation now matches current implementation

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@ncipollina
ncipollina requested a review from Copilot July 9, 2025 00:09
@ncipollina
ncipollina merged commit e9a3f73 into main Jul 9, 2025
1 check passed
@ncipollina
ncipollina deleted the feature/lambda-configurable-resources-and-function-urls branch July 9, 2025 00:10

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR makes the Lambda construct’s memory size and timeout configurable and adds optional Function URL support, updating tests and documentation accordingly.

  • Expose MemorySize, TimeoutInSeconds, and GenerateUrl in props and builder
  • Wire through configuration into LambdaFunctionConstruct and add new function URL logic
  • Update test fixtures, assertions, and README to cover and document the new options

Reviewed Changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
test/.../LambdaFunctionConstructCustomization.cs Pass new generateUrl flag into AutoFixture customizer
test/.../LambdaFunctionConstructAutoDataAttribute.cs Accept generateUrl parameter for auto data attribute
test/LambdaFunctionConstructTests.cs Add and update tests for memory, timeout, and URLs
src/.../LambdaFunctionConstructPropsBuilder.cs Introduce WithMemorySize, WithTimeoutInSeconds, and WithGenerateUrl builder methods
src/.../LambdaFunctionConstructAssertions.cs Add assertion helpers for memory, timeout, and URLs
src/.../Models/LambdaFunctionConstructProps.cs Define new props with default memory, timeout, and URL flag
src/.../LambdaFunctionConstruct.cs Apply configurable memory/timeout and conditional URL setup
README.md Document new options and examples
Comments suppressed due to low confidence (2)

src/LayeredCraft.Cdk.Constructs/Models/LambdaFunctionConstructProps.cs:12

  • MemorySize and TimeoutInSeconds are declared as double but AWS Lambda memory and timeout values must be integers. Consider using int types to improve type safety and prevent fractional values.
    double MemorySize { get; set; }

src/LayeredCraft.Cdk.Constructs/Testing/LambdaFunctionConstructPropsBuilder.cs:17

  • The builder stores memory and timeout values as double. Consider using int to match the underlying integer-based AWS Lambda configuration and avoid fractional assignments.
    private double _memorySize = 1024;

});

if (outputs.Count != 1)
throw new Exception($"Expected 1 function URL output but found {outputs.Count}");

Copilot AI Jul 9, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Throwing a generic Exception for assertion failures can obscure test failures. Consider using a more specific assertion helper or test framework assertion to deliver clearer error messages.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants